package com.kmxd.ams.init;

import cn.hutool.core.util.ObjectUtil;
import com.kmxd.ams.app.system.command.InitCropCmd;
import com.kmxd.ams.client.system.ISysCropSvc;
import com.kmxd.ams.client.system.ISysRoleSvc;
import com.kmxd.ams.infra.system.entity.SysCrop;
import com.kmxd.ams.infra.system.entity.SysRole;
import java.util.List;
import java.util.stream.Collectors;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

/**
 * 系统启动后自动执行升级内容
 *
 * @author zuolg
 */
@Component
@Slf4j
public class InitUpgradeV3 {
  @Value("${app.upgrade}")
  private String upgrade;

  @Autowired private ISysRoleSvc sysRoleSvc;
  @Autowired private ISysCropSvc sysCropSvc;
  @Autowired private InitCropCmd initCropCmd;

  public void initMenu() {
    // 查询单位
    List<SysCrop> cropList = sysCropSvc.list();
    // 查询单位角色
    List<SysRole> roleList = sysRoleSvc.list();
    cropList.forEach(
        crop -> {
          List<SysRole> roles =
              roleList.stream()
                  .filter(role -> ObjectUtil.equals(role.getComId(), crop.getId()))
                  .collect(Collectors.toList());
          if (roles.size() > 0) {
            initCropCmd.initMenu(crop, roles);
            System.out.println("roles:" + roles.size());
          }
        });
  }

}
